package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * @author : [chenxys]
 * @version : [v1.0]
 * @Description : 用户预约管理
 * @createTime : [2022/5/26 20:26]
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private OrderService orderService;

    /**
     * 新增预约
     * @author chenxys
     * @param map
     * @return com.itheima.entity.Result
     */
    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map){
        String telephone = (String)map.get("telephone");
        //获取redis中的验证码
        String validateCodeInRedis = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        //获取提交的验证码
        String validateCode = (String) map.get("validateCode");
       //获取验证码发送的次数
        String numStr = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER + "num");
        if (numStr != null && numStr.length() > 0){
            //验证码发送次数大于5次
            if (Integer.parseInt(numStr) > 5){
                return new Result(false,MessageConstant.TOO_MANY_TIMES);
            }
        }
        //比对验证码
        if (validateCode != null && validateCodeInRedis != null && validateCode.equals(validateCodeInRedis)){
            //设置预约类型
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            Result result = null;
            try {
                ////通过Dubbo远程调用服务实现在线预约业务处理
                result = orderService.order(map);
            }catch (Exception e){
                return new Result(false,MessageConstant.VALIDATECODE_ERROR);
            }
            if (result.isFlag()){
                //预约成功，可以为用户发送预约成功短信
                try {
                    SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone, (String) map.get("orderDate"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return result;
        }else {
            //验证码不相同
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }

    /**
     * 根据ID查询预约信息
     * @author chenxys
     * @param id
     * @return com.itheima.entity.Result
     */
    @RequestMapping(value = "/findById")
    public Result findById(Integer id){
        try {
            Map map = orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        }catch (Exception e){
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }
}
